﻿using Server.Database;
using Server.Resource;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using Server.Resource;
using Server.Business;

namespace Monitor.Log
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            ReadConfig();
            this.button2.Click += button2_Click;
        }

        void button2_Click(object sender, EventArgs e)
        {
            try
            {
                Clipboard.SetText(this.richTextBox1.Text);
            }
            catch
            {
            }
        }
 

        private string FormatLog(LogDB log)
        {
            DateTime dt = Utility.DateTimeFromLong(log.Time);
            string time = dt.ToString("yyyy-MM-dd HH:mm:ss");
            return string.Format("{0} >> {1} : {2}", time, log.Action, log.Info);
        }

        private void ReadConfig()
        {
            string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ServerConfig.xml");
            XDocument doc = XDocument.Load(configPath);
            XElement root = doc.Root;

            ServerConfig.Singleton.Database = root.Element("Database").Value;
            ServerConfig.Singleton.TemplateRoot = root.Element("TemplateRoot").Value;
            ServerConfig.Singleton.ImgRoot = root.Element("ImgRoot").Value;
            ServerConfig.Singleton.ExportRoot = root.Element("ExportRoot").Value;
            ServerConfig.Singleton.ExportUrl = root.Element("ExportUrl").Value;
            ServerConfig.Singleton.Debug = root.Element("Debug").Value == "1";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var query = from r in LogService.GetLogs()
                        orderby r.Time descending
                        select r;

            StringBuilder sb = new StringBuilder();
            foreach (LogDB log in query)
            {
                sb.AppendLine(FormatLog(log));
            }
            this.richTextBox1.Text = sb.ToString();
        }
    }
}
